package top.system.dept.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.system.dept.pojo.SysResp;
import top.system.dept.service.ThingOrderService;
import top.system.dept.utils.RespUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OrderController
 * @Author: Amosen
 * @Since: 2021/5/21
 */

@Controller
@RequestMapping("/order")
@ResponseBody
public class OrderController {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Autowired
    ThingOrderService thingOrderService;

    // items的格式：[{ "itemid": 0, "number": 10}, { "itemid": 1, "number": 20 }]
    @RequestMapping(value = "/thing/generate", method = {RequestMethod.POST})
    @ResponseBody
    public SysResp generateOrder(HttpServletRequest request) {
        String items = request.getParameter("items");
        String userModal = request.getParameter("userModal");
        String useDate = request.getParameter("useDate");
        String returnDate = request.getParameter("returnDate");
        ObjectMapper om = new ObjectMapper();
        List list = null;
        Date use = null;
        Date comeback = null;
        try {
            list = om.readValue(items, List.class);
            use = sdf.parse(useDate);
            comeback = sdf.parse(returnDate);
        } catch (Exception e) {
            return RespUtil.illegalParam();
        }

         return thingOrderService.generateOrder(list, userModal, use, comeback);

    }

    @RequestMapping(value = "/thing/submit", method = {RequestMethod.POST})
    @ResponseBody
    public SysResp handleSubmitOrder(@RequestParam("userModal") String userModal,
                                     @RequestParam("orderId") String orderId) {
        return thingOrderService.handleSubmitOrder(userModal, orderId);
    }

    @RequestMapping(value = "/thing/get", method = {RequestMethod.GET})
    @ResponseBody
    public SysResp getOrder(@RequestParam(value = "all", required = false, defaultValue = "false") boolean all,
                            @RequestParam(value = "userModal", required = true) String userModal,
                            @RequestParam(value = "orderId", required = false, defaultValue = "") String orderId) {
        return thingOrderService.getThingOrders(all, userModal, orderId);
    }

    @RequestMapping(value = "/thing/check")
    @ResponseBody
    public SysResp handleCheckResult(@RequestParam("orderId") String orderId,
                                     @RequestParam("passed") boolean passed,
                                     @RequestParam(value = "toUserId", required = false) Integer toUserId,
                                     @RequestParam("userModal") String userModal) {
        return thingOrderService.handleCheckResult(userModal, orderId, passed, toUserId);
    }

    @RequestMapping("/thing/prepared")
    @ResponseBody
    public SysResp handlePrepared(@RequestParam("orderId") String orderId,
                                  @RequestParam("userModal") String userMoal,
                                  @RequestParam("result") String result) {
        return thingOrderService.handlePrepared(orderId, userMoal, result);
    }

    @RequestMapping("/thing/return")
    @ResponseBody
    public SysResp handleReturned(@RequestParam("orderId") String orderId,
                                  @RequestParam("userModal") String userModal,
                                  @RequestParam("result") String result) {
        return thingOrderService.handleReturned(orderId, userModal, result);
    }
}
